//C:8.4:swaps.cpp -- swapping with references an with pointers
#include <iostream>
void swapr(int &a, int &b);    // a, b are aliases for ints
void swapp(int *p, int *q);    // p, q are address of ints
void swapv(int a, int b);      // a, b are new variables

int main(void)
{
    using namespace std;
    int wallet1 = 300;
    int wallet2 = 350;
    cout << "wallet1 = $" << wallet1;
    cout << " wallet2 = $" << wallet2 << '\n';

    cout << "Using references to swap contents:\n";
    swapr(wallet1, wallet2);    // pass variables
    cout << "wallet1 = $" << wallet1;
    cout << "wallet2 = $" << wallet2 << '\n';

    cout << "Using pointers to swap contents again:\n";
    swapp(&wallet1, &wallet2);    // pass address of variables
    cout << "wallet1 = $" << wallet1;
    cout << "wallet2 = $" << wallet2 << '\n';

    cout << "Trying to use passing by value:\n";
    swapv(wallet1, wallet2);    // pass values of variables
    cout << "wallet1 = $" << wallet1;
    cout << "wallet2 = $" << wallet2 << '\n';
    return 0;
}

void swapr(int &a, int &b)    // use references
{
    int temp;
    temp = a;    // use a, b for values of variables
    a = b;
    b = temp;
}            

void swapp(int *p, int *q)    // use pointers
{
    int temp;
    temp = *p;
    *p = *q;
    *q = temp;
}

void swapv(int a, int b)    // try using values
{
   int temp;
   temp = a;
   a = b;
   b = temp;
}                            
